import os
import subprocess
import re
"""
adb命令封装
"""


# 获取android手机系统版本
def get_android_version():
    command = 'adb shell getprop ro.build.version.release'
    result = str(os.popen(command).read())
    if result !='':
        version = result
        return version
    else:
        return None

# 获取android连接的手机 以及序列号
def get_android_device():
    command = 'adb devices'
    device_list = []
    result = os.popen(command)
    for line in result.readlines():
        if not line.startswith("List"):
            device = str(line).split(" ")[0]
            device_list.append(device)
            return device_list

# 获取 android 手机上的包名
def get_activity_name():
    command = 'adb shell pm list packages'
    activity_name_list = []
    result = os.popen(command)
    for i in result.readlines():
        activity_name = i.strip('package')
        activity_name = activity_name.strip(':')
        activity_name = activity_name.strip('\n')
        activity_name_list.append(activity_name)
    return activity_name_list


#获取 android 手机HttpLog的日志

def get_user_token():
    order = 'adb logcat | grep HttpLog'
    pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE)
    for i in iter(pi.stdout.readline,'r'):
        le = re.findall('X-Dog-T.*:.{33}',i.decode())
        if len(le) != 0:
            X_Dog_Token = le[0][13:]
            print(X_Dog_Token)


        # aa = re.findall(r"X-Dog-Token:(.+?)'\n'", i)
        # print(aa)
        # print(i.decode())
    return 'lll'


#android 手机截屏

def get_android_screencap():
    command = 'screencap -p /sdcard/data/1.png'
    result = str(os.popen(command))
    print(result)




# 删除所有你指定包名的 APP
def delAllapp( ):
    print ('start delete all your app in your Phone or Simulator ')
    os.popen('adb wait-for-device');
    corename = input("input your app package corename:")
    oriPackages = os.popen('adb shell pm list packages {name}'.format(name=corename));
    # list all PackageName
    for oriPackage in oriPackages:
        deletePackage = oriPackage.split(':')[1]
        os.popen('adb uninstall ' + deletePackage );
        print(deletePackage + "is deleted")

#清理本地缓存

def reset_local_cache():
    order = 'adb devices'  # 获取连接设备
    pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE)
    res = pi.stdout.read().decode()
    res_num = res.find("device",9,len(res)-1)
    if res_num == -1:
        result_num = '未查找到连接的手机'
        print(result_num)
    else:
        os.popen('adb shell pm clear com.cyqxx.puzzle.idiom.cn')
        result_num = '清理本地缓存成功'
        print(result_num)


def reset_level():
    order = 'adb devices'  # 获取连接设备
    pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE)
    res = pi.stdout.read().decode()
    res_num = res.find("device", 9, len(res) - 1)
    if res_num == -1:
        result_num = '未查找到连接的手机'
    else:
        os.popen('adb shell dumpsys battery set level 1')
        result_num = '手机电量已模拟到%1'

def get_user_id():
    order = 'adb devices'  # 获取连接设备
    pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE)
    res = pi.stdout.read().decode()
    res_num = res.find("device", 9, len(res) - 1)
    if res_num == -1:
        result_num = '未查找到连接的手机'
    else:
        order = os.popen('adb logcat | grep user_id')
        pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE)
        for i in iter(pi.stdout.readline, 'r'):
            print(i.decode())

get_user_token()